﻿using EasyAdmin.DatabaseAccessor.SqlSugar.Seeds;
using EasyAdmin.Domain.Entities.Systems;
using EasyCore.Extensions;
using log4net.Core;
using Masuit.Tools.Security;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Domain.Seeds
{
    public class GlobalSqlSugarSeedData : ISqlSugarSeedData<SysOrg>
    {
        /// <summary>
        /// 全局种子函数调用
        /// </summary>
        /// <param name="sqlSugarClient"></param>
        /// <exception cref="NotImplementedException"></exception>
        public async void ProducedSeedData(ISqlSugarClient sqlSugarClient)
        {
            //添加机构信息
            var org = await sqlSugarClient.Queryable<SysOrg>().FirstAsync(m => m.Name == "根运维管理部门" && m.Level == 1);
            if (org == null) {
                org = new SysOrg()
                {
                    ParentId = 0,
                    Level = 1,
                    OrgCode = "0.1",
                    DeptCode = 1,
                    Name = "根运维管理部门",
                    FullName = "根运维管理部门（虚似部门）",
                    OrgType = Shared.Enums.OrgType.Company,
                    IsDisabled = false,                   
                };
                await sqlSugarClient.Insertable<SysOrg>(org).ExecuteCommandAsync();//执行数据插入操作
                org = await sqlSugarClient.Queryable<SysOrg>().FirstAsync(m => m.Name == "根运维管理部门" && m.Level == 1);
            }

            //添加角色信息
            var role = await sqlSugarClient.Queryable<SysRole>().FirstAsync(m => m.RoleCode == "super");
            if (role == null) {
                role = new SysRole()
                {
                    RoleCode = "super",
                    RoleName = "超级管理员",
                    OrderSort = 1
                };
                await sqlSugarClient.Insertable<SysRole>(role).ExecuteCommandAsync();   
                role = await sqlSugarClient.Queryable<SysRole>().FirstAsync(m => m.RoleCode == "super");
            }

            //添加用户信息
            var user = await sqlSugarClient.Queryable<SysUser>().FirstAsync(m=>m.Account == "admin");
            if (user == null)
            {
                user = new SysUser()
                {
                    Name = "超级管理员",
                    Gender = Shared.Enums.Gender.Male,
                    Nickname = "超级飞侠",
                    Account = "admin",
                    Password = "admin".SHA256(),
                    OrgId = org.Id,
                    IsDisabled = false,
                };
                await sqlSugarClient.Insertable<SysUser>(user).ExecuteCommandAsync();
                user = await sqlSugarClient.Queryable<SysUser>().FirstAsync(m => m.Account == "admin");
                var userRole = new SysUserRole()
                {
                    UserId = user.Id,
                    RoleId = role.Id
                };
                await sqlSugarClient.Insertable(userRole).ExecuteCommandAsync();  //插入用户角色信息     


            }




















        }
    }
}
